//
//  SHUserCenterHttp.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/6/1.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "UserModel.h"
#import "SHBankInfoModel.h"
#import "SHUserAuthStatusModel.h"
#import "SHUserCenterHttp.h"

@implementation SHUserCenterHttp



#pragma mark - 获取用户资料
#pragma mark - 获取用户资料
+ (void)requstUserInfoSuccess:(void(^)( UserModel *userModel ))success  complection:(void(^)(id result))complection      failure:(void(^)(id erro))failure{
    
    [MyDataService requestAFURL:Klogin  httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        
        if ([result[@"success"]isEqualToNumber:@(1)] && [result[@"data"] count] != 0 ) {
        

             UserModel *userModel = [UserModel mj_objectWithKeyValues:result[@"data"]];
            [[UserModel shareManager] setMyUserModel:userModel];
            //统计用户ID
            int userId = userModel._id ? userModel._id: 1;
            NSString *userPuid = [NSString stringWithFormat:@"%d",userId];            

             success(userModel);
          
        }else{
            if ([result[@"data"] isKindOfClass:[NSString class]]) {
                 [[UIApplication sharedApplication].keyWindow makeToast:result[@"data"] duration:2.f position:@"center"];
                complection(result);
            }
           
        }
        
    } failure:^(id erro) {
        failure(erro);
    }];
    
}


#pragma mark -- 验证绑卡状态
+ (void)checkCardBundingStatus:(NSString *)cardBundingStatus   success:(void(^)(int  status))success   failure:(void(^)(int  status))failure {
    
    UIViewController *vc = [SHHelper currentViewController];
    
    int  status = [cardBundingStatus intValue];
    switch (status) {
        case 0:
        {
            [vc.view makeToast:@"尚未绑定银行卡,立即申请。" duration:2 position:@"center"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                failure(status);
                
            });
        }
            return;
        case 1:
            [vc.view  makeToast:@"银行卡绑定审核中。" duration:2 position:@"center"];
            return;
        case 2://可以刷卡
            success(status);
            
            
            return;
        case 3:
        {
            [vc.view makeToast:@"银行卡绑定审核未通过,请重新绑定银行卡。" duration:2 position:@"center"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                failure(status);
                
            });
        }
            
            return;
       
    }
    
    
}

#pragma mark - 获取支行号
+ (void)getBankCnapsCodeBankProId:(NSString *)bankProId  bankCityId:(NSString *)bankCityId success:(void(^)(NSMutableArray *bankInfoArray))success  complection:(void(^)(id result))complection{
    
    if ([Tool objectForKey:[NSString stringWithFormat:@"%@-%@",bankProId,bankCityId]]) {
        NSMutableArray *  bankInfoArray  = [SHBankInfoModel mj_objectArrayWithKeyValuesArray:[Tool objectForKey:[NSString stringWithFormat:@"%@-%@",bankProId,bankCityId]]];
        success(bankInfoArray);

        return;
    }
    UIViewController *vc = [SHHelper currentViewController];
    [MBProgressHUD showHUDAddedTo:vc.view animated:YES];

    NSDictionary *params = @{@"bankProId":bankProId,@"bankCityId":bankCityId};
    [MyDataService requestNoRSAAFURL:@"/data/api/get_bank_cnaps_code" httpMethod:@"GET" params:params data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:vc.view animated:YES];

        if ([result isKindOfClass:[NSDictionary class]]) {
            BOOL succesed = [result[@"success"]boolValue];
             if (succesed) {
      
           NSMutableArray *  bankInfoArray  = [SHBankInfoModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
            [Tool setObject:result[@"data"] forKey:[NSString stringWithFormat:@"%@-%@",bankProId,bankCityId]];
                 success(bankInfoArray);
             }else{
                 complection(result);
                 [vc.view makeToast:result[@"data"] duration:1.5 position:@"center"];
             }

        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:vc.view animated:YES];


    }];
    
    
    
    
}



#pragma mark - 查询用户实名认证状态 绑卡状态
+ (void)findSHUserAuthBindBankStatusSuccess:(void (^)(NSNumber * authStatus, NSNumber *bankStatus))suceess{
   
   
    [MyDataService requestAFURL:KGETAUTHBINDSTATUS httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        SHLog(@"查询实名认证状态%@",result);
        BOOL succesed = [result[@"success"] boolValue];
        if (succesed) {
            if ([result[@"data"] count] != 0) {
                SHUserAuthStatusModel *authStatusModel = [SHUserAuthStatusModel defaultUser];
                [authStatusModel setValuesForKeysWithDictionary:result[@"data"]];
                suceess(authStatusModel.auth_status,authStatusModel.bank_status);

            }
        }else{
            
            [[SHHelper currentViewController].view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
    } failure:^(id erro) {
        
    }];
    
    
}


#pragma mark -- 验证SH实名认证状态
+ (void)checkSHUserAuthStatus:(NSNumber *)authStatus success:(void(^)(int  status))success      failure:(void(^)(int  status))failure {
    UIViewController *vc = [SHHelper currentViewController];
    int  status = [authStatus intValue];
    switch (status) {
        case 0://尚未实名认证
        {
            [vc.view makeToast:@"未实名认证,立即认证" duration:2 position:@"center"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                failure(status);
                
            });
        }
            return;
        case 1://实名认证成功
            success(status);
            return;
            
    }
    
    
}

#pragma mark -- 验证SH绑卡状态
+ (void)checkSHUserBindCardStatus:(NSNumber *)bindBankStatus success:(void(^)(int  status))success      failure:(void(^)(int  status))failure {
    UIViewController *vc = [SHHelper currentViewController];
    int  status = [bindBankStatus intValue];
    switch (status) {
        case 0://未绑卡
        {
            [vc.view makeToast:@"未添加银行卡" duration:2 position:@"center"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                failure(status);
                
            });
        }
            return;
        case 1://绑卡成功
            
            success(status);
            return;
            
        case 5://杉德用户绑卡审核中】
            [vc.view makeToast:@"杉德收款卡审核中。" duration:2 position:@"center"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                failure(status);
                
            });
            return;
            
    }
    
    
}





#pragma mark -- 查询SH用户实名认证状态
+ (void)finderSHUserAuthStatusSuccess:(void(^)(int  status))success   failure:(void(^)(int  status))failure{
   
   
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        
        [self findSHUserAuthBindBankStatusSuccess:^(NSNumber *authStatus, NSNumber *bankStatus) {
        [self checkSHUserAuthStatus:authStatus success:^(int status) {//实名
            
            success(status);
            
        } failure:^(int status) {//未实名认证
            failure(status);
        }];
        
    }];

    });
    
}


#pragma mark -- 查询SH用户绑卡状态

+ (void)finderSHUserBindCardStatusSuccess:(void(^)(int  status))success    complection:(void(^)(int status))complection    failure:(void(^)(int  status))failure{
 

    [self finderSHUserAuthStatusSuccess:^(int status) {//已经实名
        [self checkSHUserBindCardStatus:[SHUserAuthStatusModel defaultUser].bank_status  success:^(int status) {//去修改卡
            success(status);
        } failure:^(int status) {//去添加卡
            complection(status);
        }];
        
    } failure:^(int status) {//未实名---去实名
        failure(status);
    }];
    
}







#pragma mark -- 获取银行卡列表
+ (void)getBankCardLsitWithUsingChoices:(int)usingChoices bankCardType:(int)bankCardType success:(void (^)(NSMutableArray *bankCardListArray))success   complection:(void(^)(id result))complection      failure:(void(^)(id erro))failure{
    
    NSDictionary *parasm = @{@"using_choices":@(usingChoices),@"bank_card_type":@(bankCardType)};
    [MyDataService requestAFURL:KNEWBANKINFO httpMethod:@"GET" params:parasm data:nil complection:^(id result) {
        SHLog(@"获取银行卡列表 %@ --- %@",parasm,result);
        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
         NSMutableArray *bankCardArray = [BankCardModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
            
            success(bankCardArray);
        }else{
            complection(result[@"data"]);
            [[SHHelper currentViewController].view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
    } failure:^(id erro) {
        failure(erro);
    }];
    
}

#pragma mark - 获取海德银行卡的列表


#pragma mark -- 添加，修改银行卡
+ (void)postBankCardInfoWithOrgCardCode:(NSString *)orgCardCode bankCardCode:(NSString *)bankCardCode bankCardFrontPic:(NSString *)bankCardFrontPic bankCardBackPic:(NSString *)bankCardBackPic  province:(NSString *)province city:(NSString *)city bankName:(NSString *)bankName phone:(NSString *)phone cnapsCode:(NSString *)cnapsCode subBankName:(NSString *)subBankName bankCardType:(int)bankCardType usingChoices:(int)usingChoices  success:(void (^)(id result))success   complection:(void(^)(id result))complection      failure:(void(^)(id erro))failure{
    
    NSDictionary *params = @{@"old_card_code":orgCardCode,@"bank_card_code":bankCardCode,@"bank_card_front_pic":bankCardFrontPic,@"bank_card_back_pic":bankCardBackPic,@"province":province,@"city":city,@"bank_name":bankName,@"phone":phone,@"cnaps_code":cnapsCode,@"sub_bank_name":subBankName,@"bank_card_type":@(bankCardType),@"using_choices":@(usingChoices)
                             };
    
    [MyDataService requestAFURL:KNEWBANKINFO httpMethod:@"POST" params:params data:nil complection:^(id result) {
        SHLog(@"添加，修改银行卡%@",result);
        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
            [[SHHelper currentViewController].view makeToast:@"提交成功！" duration:2.f position:@"center"];
            
            success(result);
        }else{
            complection(result[@"data"]);
            [[SHHelper currentViewController].view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        

    } failure:^(id erro) {
        failure(erro);

    }];
     
    
}

#pragma mark -- 删除银行卡
+ (void)deleteBankCardWithCardId:(NSString *)cardId success:(void (^)(id result))success   complection:(void(^)(id result))complection      failure:(void(^)(id erro))failure{
    
    [MyDataService requestAFURL:[NSString stringWithFormat:@"%@?card_id=%@",KNEWBANKINFO,cardId] httpMethod:@"DELETE" params:nil data:nil complection:^(id result) {
        SHLog(@"删除银行卡 %@ ",result);
        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
            [[SHHelper currentViewController].view makeToast:@"删除成功！" duration:2.f position:@"center"];

            success(result);
        }else{
            complection(result[@"data"]);
            [[SHHelper currentViewController].view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
    } failure:^(id erro) {
        failure(erro);
    }];
    

    
    
}


#pragma mark -- 设置为默认卡

+ (void)putDefaultBankCardWithCardId:(NSString *)cardId  usingChoices:(int)usingChoices  success:(void (^)(id result))success   complection:(void(^)(id result))complection      failure:(void(^)(id erro))failure{
    //NSDictionary *params = @{@"card_id":cardId,@"using_choices":@(usingChoices)};
    [MyDataService requestAFURL:[NSString stringWithFormat:KPUTDEFAULTBANKCARD,cardId,usingChoices] httpMethod:@"PUT" params:nil data:nil complection:^(id result) {
        SHLog(@"设置为默认银行卡 %@",result);
        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
            [[SHHelper currentViewController].view makeToast:@"设置为默认银行卡" duration:2.f position:@"center"];
            
            success(result);
        }else{
            complection(result[@"data"]);
            [[SHHelper currentViewController].view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
    } failure:^(id erro) {
        failure(erro);
    }];
    
    
    
    
}

#pragma mark -- 修改认证成功的卡的手机号
+ (void)putBankCardPhoneWithCardNo:(NSString *)cardNo phone:(NSString *)phone  success:(void (^)(id result))success   complection:(void(^)(id result))complection      failure:(void(^)(id erro))failure{
    [MyDataService requestAFURL:[NSString stringWithFormat:@"%@?bank_card_code=%@&phone=%@",KNEWBANKINFO,cardNo,phone] httpMethod:@"PUT" params:nil data:nil complection:^(id result) {
        SHLog(@"修改认证成功的卡的手机号 %@",result);
        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
            [[SHHelper currentViewController].view makeToast:@"修改手机号成功" duration:2.f position:@"center"];
            
            success(result);
        }else{
            complection(result[@"data"]);
            [[SHHelper currentViewController].view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
    } failure:^(id erro) {
        failure(erro);
    }];
    

    
}

@end
